requireJS(['constant', 'site', 'handlebars', 'public','static/js/page/common/common.js'], function (Constant, site, Handlebars, Public, Common) {
    Public.initProductSearch();
    Constant.initHandlebars(Handlebars);

    var url = {
        queryOrder: 'manager/selectOrderMortgageList.do',
        activOrder: 'manager/activOrder.do',
        selectLoanMethodList: "manager/selectLoanMethodList.do",
    }

    //渲染搜索框的贷款方式
    site.send({
        url: url.selectLoanMethodList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#loanMethod_list_tpl").html();
                var template = Handlebars.compile(source);
                var list = data
                var html = template(list)
                $('#loan_way_select').append(html);
            }
        }
    })

    var order_id;
    //查询阐述
    var frontData = {
        pageNum: 1
    }
    var allDownLoadData = {};


    // 初始化
    var all_select = function () {
        //头部日期
        site.send({
            url: "manager/selectApplyTermList.do",
            async: false,
            data: {},
            callback: function (data) {
                var list = JSON.parse(data)

                var template2 = Handlebars.compile($("#contract_head_date_tpl").html())
                var html2 = template2(list)
                $("#contract_head_date").html(html2)

            }
        })
        //请求服务端查询待签订合同的订单
        site.send({
            // async: false,
            url: url.queryOrder,
            data: frontData,
            callback: function (data) {
                if (data.msg == '系统异常') {
                    Constant.fn.Message('F', data.msg)
                }
                var list = JSON.parse(data)
                if (list.type == 1) {
                    var source = $("#contract_management_tpl").html();
                    var template = Handlebars.compile(source);


                    var menuIndex = getAurlparam('h') || 0;
                    var thirdMenusIndex = getAurlparam('t') || 0;
                    var leftIndex = getAurlparam('i') || 0;

                    var storageContent = JSON.parse(localStorage.getItem('content'));

                    var obj = {
                        content: list,
                        fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                    }

                    var html = template(obj);
                    $("#contract_management").html(html);
                    if (list.content) {
                        $("#total").text(list.content.total)
                        $('.public-botPage-right').createPager({
                            current: frontData.pageNum,
                            total: list.content.total,
                            pageSize: list.content.pageSize,
                            needInput: false,
                            backFn: function (current) {
                                frontData.pageNum = current;
                                all_select();
                            }
                        })
                    }
                    // 分页
                    Public.record('t_order_public');
                } else {
                    Constant.fn.Message('F', list.msg)
                }                // console.log(' contract_management ' + data);

                onBindFun();
            }
        });


    }


    all_select();

    function onBindFun() {
        //select操作
        $(".operation_list").on('change', function () {
            var obj = $(this).find("option:selected");
            if (obj.val() == 'activeOrder') {
                $('.cover_winBox').toggle();
                $('.coverRemarks').val('');
                order_id = $(this).data('oid')
            }
            // 待签合同
            if (obj.val() == '3') {
                $(".determineWinbox").show();
                determineFun(obj.data('id'));
            }

            //查看订单
            if (obj.val() == 'checkOrder') {
                pjaxGo('loanIng/contractManager/detail.html?order_id=' + $(this).data('id') + "&product_id=" + $(this).data('productid') + "&task_id=" + $(this).data('taskid'));
            }

            if (obj.val() == 'submitVisit') {
                Common.visit(order_id);
                Common.init(3,order_id);
            }

            $(this).find("option:first").attr("selected", false).attr("selected", true);
        })
        //拿到所有的复选框，当有复选框没选上的时候,取消全选
        $("input:checkbox[name='ch_id']").on('click', function () {
            var checkboxs = $("input:checkbox[name='ch_id']");
            var sum = 0;
            var length = checkboxs.length;
            $.each(checkboxs, function (i, checkbox) {
                if ($(checkbox).prop("checked")) {
                    sum++;
                }
            })
            if (sum == length) {
                $('.all_check').prop("checked", true);
            } else {
                $('.all_check').prop("checked", false);
            }
        })
        // 全选
        $(".all_check").click(function () {
            $("input[name='ch_id']").prop('checked', $(this).prop('checked'))
        })
        //批量操作恢复订单select
        $("#allDown_select").on('change', function () {
            /*var obj = $(this).find("option:selected");
            // 下载合同
            if (obj.val() == '2') {
                allDownLoadData.oid = '';
                var inp = $("input[name='ch_id']");//获得所有check按钮
                var count = 0;
                for (var i = 0; i < inp.length; i++) {
                    if (inp[i].checked) {
                        count++;
                        allDownLoadData.oid = inp[i].value;
                        allDownLoad();
                    }
                    if (count == 0) {
                        Constant.fn.Message("I", "未选择")
                    }
                }

                $("#allDown_select").val('-1')

            }*/
            order_id = "";

            var selected = $(this).find("option:selected");
            $(this).find("option:first").prop("selected", false).prop("selected", true);
            $("input[name='ch_id']:checked").each(function (index, item) {
                order_id = order_id + $(item).val() + ","
            })
            order_id = order_id.substring(0, order_id.length - 1)
            if (selected.val() == '恢复订单') {
                //删除产品
                if (!order_id) {
                    Constant.fn.Message('I', "未选择要恢复的订单")
                    return;
                }

                $('.cover_winBox').toggle();

                /*$('.sureCover').unbind().on('click', function () {
                    var remarks = $('.coverRemarks').val();
                    if (!remarks) {
                        Constant.fn.Message('I', '请输入备注')
                        return;
                    }*/
                //})
            }
        })
    }

    $('.sureCover').on('click', function () {
        var remarks = $('.coverRemarks').val();
        if (!remarks) {
            Constant.fn.Message('I', '请输入备注')
            return;
        }
        site.send({
            url: url.activOrder,
            data: {
                oid: order_id,
                remarks: remarks
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    $('.cover_winBox').toggle();
                    Constant.fn.Message("T", data.msg);
                    pjaxReload();
                } else {
                    Constant.fn.Message("F", data.msg);
                }
            }
        })
    })

    //待签合同确认
    function determineFun(id) {
        $("#determineBtn").click(function () {
            site.send({
                async: false,
                url: 'manager/updateOrderStatus.do',
                data: {
                    pageNum: 1,
                    id: id,
                    status: 1
                },
                callback: function (data) {
                    if (data.msg == '系统异常') {
                        Constant.fn.Message('F', data.msg)
                    }
                    var list = JSON.parse(data)
                    if (list.type == 1) {
                        $(".determineWinbox").hide();
                        all_select();
                    } else {
                        Constant.fn.Message('F', list.msg)
                    }
                }
            })
        })
    }

    // 搜索
    $(".serach_orderMortgageList").click(function () {
        frontData = {
            pageNum: frontData.pageNum
        }
        if ($("#start_time").val() != '') {
            frontData.beginTime = $("#start_time").val();
        }
        if ($("#end_time").val() != '') {
            frontData.endTime = $("#end_time").val();
        }
        if ($("#seleNames").val() != '') {
            frontData.type = $("#seleNames").val();
        }
        if ($("#inputNames").val() != '' && $("#inputNames").val().trim()) {
            frontData.keyword = $("#inputNames").val();
        }
        if ($(".data_select").val() != '') {
            frontData.atId = $(".data_select").val();
        }
        if ($("#loan_way_select").val() != '') {
            frontData.loan_way = $("#loan_way_select").val();
        }
        if($("#product_choose_list").val()){
            frontData.product_id = $("#product_choose_list").val()
        }
        all_select();
    })
    //日期控件
    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true
    })


    // 批量下载
    function allDownLoad() {
        site.send({
            url: 'manager/contractBatchDownload.do',//
            // async: false,
            data: allDownLoadData,
            callback: function (data) {
                var list = JSON.parse(data);
                if (list.type == 1) {
                    downloadFile(list.content.zipUrl)
                } else {
                    Constant.fn.Message('F', list.msg)
                }
            }
        })
    }

    //下载合同方法
    function downloadFile(src) {
        var $a = document.createElement('a');
        $a.setAttribute("href", src);
        $a.setAttribute("download", "");
        var evObj = document.createEvent('MouseEvents');
        evObj.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, true, false, 0, null);
        $a.dispatchEvent(evObj);
    };


    $(".canCover").on('click', function () {
        $(this).parents('.winBox').hide();
    })
})